/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.max.oti.system.util.lazy;

import com.max.oti.system.dao.interfac.IHistoryPracticeDao;
import com.max.oti.system.model.HistoryPractice;
import java.util.List;
import java.util.Map;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

/**
 *
 * @author max
 */
public class LazyHPDataModel extends LazyDataModel<HistoryPractice> {

    @Autowired
    @Qualifier("historyPracticeDao")
    IHistoryPracticeDao historyPracticeDao;
    ///////////////////////////////////////////////
    private List<HistoryPractice> dataSource;

    @Override
    public HistoryPractice getRowData(String rowKey) {
        if (dataSource == null) {
            return null;
        }
        for (int i = 0; i < dataSource.size(); i++) {
            if (dataSource.get(i).getId().equals(Integer.parseInt(rowKey))) {
                return dataSource.get(i);
            }
        }
        return null;
    }

    @Override
    public List<HistoryPractice> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
        try {
            dataSource = historyPracticeDao.listHQL("from HistoryPractice p where p.oldHistory='false'", first, pageSize);

            // set the total of players
            if (getRowCount() <= 0) {
                setRowCount(Integer.parseInt(historyPracticeDao.listHQL("select count(p.id) HistotyPractice p where p.oldHistory='false'").get(0) + ""));
            }
            setPageSize(pageSize);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Object getRowKey(HistoryPractice object) {
        return object.getId();
    }
}
